<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入搜索内容" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <el-button type="primary" @click="exportList">导出列表</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="提交用户" width="100" header-align="center" align="center"/>
        <span v-for="(item, index) in formInfo" :key="index">
          <el-table-column v-if="item.type==='UploadPicture'" :prop="`formItem${index}`" :label="item.name" min-width="200" header-align="center" align="center">
            <template slot-scope="scope">
              <el-image v-for="(img, imgIdx) in scope.row[`formItem${index}`]" :key="imgIdx" :src="img" :preview-src-list="scope.row[`formItem${index}`]" style="width: 80px;height: 80px;"/>
            </template>
          </el-table-column>
          <el-table-column v-else-if="item.type==='UploadFile'" :prop="`formItem${index}`" :label="item.name" min-width="200" header-align="center" align="center">
            <template slot-scope="scope">
              <el-link :href="Object(scope.row[`formItem${index}`])['url']" target="_blank">{{ Object(scope.row[`formItem${index}`])['filename'] }}</el-link>
            </template>
          </el-table-column>
          <el-table-column v-else-if="item.type==='UploadVoice'" :prop="`formItem${index}`" :label="item.name" min-width="200" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small">播放</el-button>
            </template>
          </el-table-column>
          <el-table-column v-else :prop="`formItem${index}`" :label="item.name" min-width="120" header-align="center" align="center"/>
        </span>
        <el-table-column prop="createTime" label="申请时间" width="180" header-align="center" align="center"/>
        <!--<el-table-column label="操作" width="200" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <router-link to="./modify">
              <el-button type="success" class="bg-green" size="small">编辑</el-button>
            </router-link>
            <el-button type="danger" size="small" @click="deleteForm(scope.row)">删除</el-button>
          </template>
        </el-table-column>-->
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleCurrentChange, handleSizeChange, handleSortChange } from '@/utils/table'

export default {
  data() {
    return {
      searchTerms: {
        id: '',
        searchValue: ''
      },

      formInfo: {
        name: '',
        content: [{
          type: 'SingleInput',
          name: '名称'
        }]
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {

  },
  created() {
    this.searchTerms.id = this.$route.query.id;
    this.getData();
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'apply/getApplyRecordList'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue,
        id: this.searchTerms.id
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.formInfo = res.data.formData;
        this.tableData = res.data.rows;
        this.total = res.data.total;
        this.tableLoading = false;
      }).catch(e => {
        this.tableLoading = false;
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    exportList() {
      window.open(this.$store.getters.serverUrl + '/admin/apply/punchCardRecordToExcel?token=' + this.$store.getters.token + '&id=' + this.searchTerms.id + '&mallId=' + this.$store.getters.SaaSId)
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  max-width: 1400px;
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
    }
    .el-select{
      width: 140px;
      margin-right: 10px;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
</style>
